/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.ledger.je.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.redragon.framework.springmvc.ControllerSupport;
import org.apache.commons.lang.StringUtils;
import org.redragon.framework.hibernate.data.SnowFlake;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.JsonDataUtil;
import org.redragon.framework.util.JsonUtil;
import org.redragon.framework.util.ShiroUtil;

import com.erp.dataset.service.DatasetCommonService;
import com.erp.hr.dao.model.HrStaffInfoRO;
import com.erp.hr.service.HrCommonService;
import com.erp.ledger.account.dao.model.GLLedger;
import com.erp.ledger.account.service.GLCodeCombinationsService;
import com.erp.ledger.account.service.GLLedgerService;
import com.erp.ledger.je.dao.data.DataBox;
import com.erp.ledger.je.dao.model.GLJeModelHead;
import com.erp.ledger.je.dao.model.GLJeModelHeadCO;
import com.erp.ledger.je.dao.model.GLJeModelLine;
import com.erp.ledger.je.service.GLJeHeadService;
import com.erp.ledger.je.service.GLJeLineService;
import com.erp.ledger.je.service.GLJeModelHeadService;
import com.erp.ledger.je.service.GLJeModelLineService;
import com.erp.masterdata.common.service.MasterDataCommonService;

@Controller
@RequestMapping("/web/gLJeModelHead")
public class GLJeModelHeadWebController extends ControllerSupport{
    
    //日志处理
    private Logger logger = LoggerFactory.getLogger(GLJeModelHeadWebController.class);
    
    //服务层注入
    @Autowired
    private GLJeModelHeadService gLJeModelHeadService;
    @Autowired
    private GLJeModelLineService glJeModelLineService;
    @Autowired
    private GLJeHeadService glJeHeadService;
    @Autowired
    private GLJeLineService glJeLineService;
    @Autowired
    private DatasetCommonService datasetCommonService;
    @Autowired
    private HrCommonService hrCommonService;
    @Autowired
    private MasterDataCommonService masterDataCommonService;
    @Autowired
    private GLCodeCombinationsService glCodeCombinationsService;
    @Autowired
    private GLLedgerService glLedgerService;
    
    @Override
    public String getExceptionRedirectUrl() {
        return this.getFormatRedirectUrl("getGLJeModelHeadList");
    }
    
    
    
    /**
     * 
     * @description 查询数据列表
     * @date 2021-01-30 14:59:00
     * @author 
     * @param pages
     * @param gLJeModelHeadCO
     * @param model
     * @return String
     *
     */
    @RequestMapping("getGLJeModelHeadList")
    public String getGLJeModelHeadList(Pages pages, GLJeModelHeadCO gLJeModelHeadCO, Model model) {
        //分页查询数据
        if(pages.getPage()==0) {
            pages.setPage(1);
        }
        
        //分页查询数据
        List<GLJeModelHead> gLJeModelHeadList = this.gLJeModelHeadService.getDataObjects(pages, gLJeModelHeadCO);
        
        //循环设置职员和组织信息
        for(GLJeModelHead glJeModelHead: gLJeModelHeadList) {
            glJeModelHead.setStaffName(this.hrCommonService.getHrStaff(glJeModelHead.getStaffCode()).getStaffName());
            glJeModelHead.setDepartmentName(this.hrCommonService.getHrDepartment(glJeModelHead.getDepartmentCode()).getDepartmentName());
        }
        
        //获取日记账类型
        Map jeTypeMap = DataBox.getJeType();
        //获取日记账来源
        Map jeSourceMap = DataBox.getJeSource();
        
        //页面属性设置
        model.addAttribute("gLJeModelHeadList", gLJeModelHeadList);
        model.addAttribute("pages", pages);
        model.addAttribute("jeTypeMap", jeTypeMap);
        model.addAttribute("jeSourceMap", jeSourceMap);
        
        return "basic.jsp?content=gLJe/jeModelList";
    }
    
    
    
    /**
     * 
     * @description 查询单条数据
     * @date 2021-01-30 14:59:00
     * @author 
     * @param gLJeModelHead
     * @param model
     * @return String
     *
     */
    @RequestMapping("getGLJeModelHead")
    public String getGLJeModelHead(GLJeModelHead gLJeModelHead, Model model) {
        List<GLJeModelLine> glJeModelLineList = new ArrayList<GLJeModelLine>();
        
        //查询要编辑的数据
        if(gLJeModelHead!=null&&gLJeModelHead.getJeHeadId()!=null&&StringUtils.isNotBlank(gLJeModelHead.getJeHeadCode())) {
            gLJeModelHead = this.gLJeModelHeadService.getDataObject(gLJeModelHead.getJeHeadId());
            //设置显示字段
            gLJeModelHead.setStaffName(this.hrCommonService.getHrStaff(gLJeModelHead.getStaffCode()).getStaffName());
            gLJeModelHead.setDepartmentName(this.hrCommonService.getHrDepartment(gLJeModelHead.getDepartmentCode()).getDepartmentName());
            //获取凭证行
            glJeModelLineList = this.glJeModelLineService.getGLJeModelLineListByHeadCode(gLJeModelHead.getJeHeadCode());
            //循环设置设置科目
            for(GLJeModelLine line: glJeModelLineList) {
                //line.setSubjectDesc(this.masterDataCommonService.getSubjectMap().get(line.getSubjectCode()));
                line.setSubjectDesc(this.glCodeCombinationsService.getGLCodeCombinationsSegment(line.getSubjectCode()));
                line.setSubjectDescTitle(this.glCodeCombinationsService.getGLCodeCombinationsSegmentDesc(line.getSubjectCode()));
            }
        }else {
            //初始化默认字段
            
            //获取当前用户职员信息
            HrStaffInfoRO staffInfo = this.hrCommonService.getStaffInfo(ShiroUtil.getPrincipal());
            gLJeModelHead.setStaffCode(staffInfo.getStaffCode());
            gLJeModelHead.setDepartmentCode(staffInfo.getDepartmentCode());
            gLJeModelHead.setStaffName(staffInfo.getStaffName());
            gLJeModelHead.setDepartmentName(staffInfo.getDepartmentName());
        }
        
        //分类账
        GLLedger glLedger = this.glLedgerService.getPrimaryGLLedgerByLegal(ShiroUtil.getLegalCode());
        
        //页面属性设置
        model.addAttribute("gLJeModelHead", gLJeModelHead);
        model.addAttribute("glJeModelLineList", glJeModelLineList);
        model.addAttribute("glLedger", glLedger);
        
        return "basic.jsp?content=gLJe/jeModelEdit";
    }
    
    
    
    /**
     * 
     * @description 编辑数据
     * @date 2021-01-30 14:59:00
     * @author 
     * @param gLJeModelHead
     * @param bindingResult
     * @param model
     * @return String
     *
     */
    @RequestMapping("editGLJeModelHead")
    public String editGLJeModelHead(@Valid GLJeModelHead glJeModelHead, BindingResult bindingResult, Integer[] jeLineId, String[] memo, String[] subjectCode, String[] drAmount, String[] crAmount, Model model, RedirectAttributes attr) {
        //参数校验
        Map<String, String> errorMap = this.validateParameters(bindingResult, model);
        if(errorMap.size()>0) {
            return "forward:getGLJeModelHead";
        }
        
        //对当前编辑的对象初始化默认的字段
        if(glJeModelHead.getJeHeadId()==null) {
            glJeModelHead.setJeHeadCode(SnowFlake.getConcurrentId().toString());
            glJeModelHead.setJeType("MANUAL");
        }
        
        List<GLJeModelLine> glJeModelLineList = new ArrayList<GLJeModelLine>();
        //循环设置凭证行
        for(int a=0;a<subjectCode.length;a++) {
            GLJeModelLine glJeModelLine = new GLJeModelLine();
            //判断是新增还是修改
            if(jeLineId[a]==null) {
                glJeModelLine.setJeLineCode(SnowFlake.getConcurrentId().toString());
                glJeModelLine.setJeHeadCode(glJeModelHead.getJeHeadCode());
            }else {
                glJeModelLine.setJeLineId(jeLineId[a]);
            }
            
            glJeModelLine.setCrAmount(crAmount[a]);
            glJeModelLine.setDrAmount(drAmount[a]);
            glJeModelLine.setMemo(memo[a]);
            glJeModelLine.setSubjectCode(subjectCode[a]);
            
            glJeModelLineList.add(glJeModelLine);
        }
        
        //保存编辑的数据
        this.gLJeModelHeadService.insertOrUpdateGLJeModel(glJeModelHead, glJeModelLineList);
        //提示信息
        attr.addFlashAttribute("hint", "success");
        
        return "redirect:getGLJeModelHeadList";
    }
    
    
    
    /**
     * 
     * @description 删除数据
     * @date 2021-01-30 14:59:00
     * @author 
     * @param gLJeModelHead
     * @return String
     *
     */
    @RequestMapping("deleteGLJeModelHead")
    public String deleteGLJeModelHead(GLJeModelHead gLJeModelHead, RedirectAttributes attr) {
        //删除数据前验证数据
        if(gLJeModelHead!=null&&gLJeModelHead.getJeHeadId()!=null&&StringUtils.isNotBlank(gLJeModelHead.getJeHeadCode())) {
            //删除数据
            this.gLJeModelHeadService.deleteDataObject(gLJeModelHead);
            //提示信息
            attr.addFlashAttribute("hint", "success");
        }
        
        return "redirect:getGLJeModelHeadList";
    }
    
    
    
    /**
     * 
     * @description 获取凭证模板Json数据
     * @date 2020年7月28日
     * @author dongbin
     * @param finVoucherHead
     * @return
     * @return String
     *
     */
    @RequestMapping("getJeModelForJson")
    @ResponseBody
    public String getJeModelForJson(GLJeModelHead glJeModelHead) {
        List<GLJeModelLine> glJeModelLineList = new ArrayList<GLJeModelLine>();
        
        //获取凭证模板头信息
        if(glJeModelHead!=null&&glJeModelHead.getJeHeadId()!=null) {
            glJeModelHead = this.gLJeModelHeadService.getDataObject(glJeModelHead.getJeHeadId());
            //获取凭证行
            glJeModelLineList = this.glJeModelLineService.getGLJeModelLineListByHeadCode(glJeModelHead.getJeHeadCode());
            //循环设置设置科目
            for(GLJeModelLine line: glJeModelLineList) {
                //line.setSubjectDesc(this.masterDataCommonService.getSubjectMap().get(line.getSubjectCode()));
                line.setSubjectDesc(this.glCodeCombinationsService.getGLCodeCombinationsSegment(line.getSubjectCode()));
            }
            glJeModelHead.setGlJeModelLineList(glJeModelLineList);
        }
        
        return JsonUtil.objectToJson(glJeModelHead, "yyyy-MM-dd");
    }
}
